<template>
	<div class="system-role-dialog-container">
		<el-dialog v-model="visible" draggable title="导入表" width="769px">
			<div class="system-role-padding layout-padding-auto layout-padding-view">
				<div class="form">
					<el-form :model="state.queryForm" :inline="true" label-width="80px">
						<el-form-item label="表名称" class="search">
							<el-input v-model="state.queryForm.keyWord" clearable size="default" placeholder="请输入表名称" style="max-width: 180px" />
						</el-form-item>
						<el-form-item class="search">
							<el-button type="primary" size="default" @click="onSubmit">
								<el-icon>
									<!-- <ele-Search /> -->
									<ma-icon icon="icon-search"></ma-icon>
								</el-icon>
								查询
							</el-button>
						</el-form-item>
					</el-form>
				</div>
				<el-table ref="multipleTableRef" v-loading="state.dataListLoading" :data="state.dataList" style="width: 100%">
					<el-table-column type="selection" width="55" />
					<el-table-column type="index" label="序号" width="60" />
					<el-table-column prop="tableName" label="表名" show-overflow-tooltip></el-table-column>
					<el-table-column prop="tableComment" label="表注释" show-overflow-tooltip> </el-table-column>
				</el-table>
				<el-pagination
					v-model:current-page="state.page"
					v-model:page-size="state.limit"
					class="mt15"
					:pager-count="5"
					:page-sizes="[10, 20, 30]"
					background
					layout="total, sizes, prev, pager, next, jumper"
					:total="state.total"
					@size-change="sizeChangeHandle"
					@current-change="currentChangeHandle"
				>
				</el-pagination>
			</div>
			<template #footer>
				<span class="dialog-footer">
					<el-button size="default" @click="onCancel">取 消</el-button>
					<el-button type="primary" size="default" @click="ok">导入</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemSourceDialog">
import { useCrud } from '@/hooks'
import { reactive, ref } from 'vue'
import { tableApi } from '@/api/sys/table'
import { ElMessage, ElLoading } from 'element-plus'
import { ElTable } from 'element-plus'
import { IHooksOptions } from '@/hooks/interface'
import { prefix } from '@/api/common'

const visible = ref(false)

const state: IHooksOptions = reactive({
	createdIsNeed: false,
	dataListUrl: `${prefix}/sys/table/unGenerateTables`,
	queryForm: {
		keyWord: ''
	}
})
const { getDataList, sizeChangeHandle, currentChangeHandle } = useCrud(state)

const ok = async () => {
	if (multipleTableRef.value?.getSelectionRows().length == 0) {
		ElMessage.warning('请选择要导入的表')
		return
	}
	let multipleSelection: any[] = []

	multipleTableRef.value!.getSelectionRows().map((v: any) => {
		multipleSelection.push(v)
	})
	const loadingInstance1 = ElLoading.service({ fullscreen: true, text: 'Loading', background: 'rgba(0, 0, 0, 0.7)' })
	tableApi.insertGentable({ tables: multipleSelection }).then(() => {
		ElMessage.success('导入成功')
		closeDialog()
		emit('refresh')
		loadingInstance1.close()
	})
}
const multipleTableRef = ref<InstanceType<typeof ElTable>>()
// 打开弹窗
const openDialog = async (type: string, row?: any) => {
	visible.value = true
	state.createdIsNeed = true
	getDataList()
}
// 关闭弹窗
const closeDialog = () => {
	visible.value = false
	state.createdIsNeed = false
}
// 取消
const onCancel = () => {
	closeDialog()
}
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh'])

// 提交
const onSubmit = async () => {
	getDataList()
}
// 暴露变量
defineExpose({
	openDialog
})
</script>

<style scoped lang="scss">
.system-role-dialog-container {
	.menu-data-tree {
		width: 100%;
		border: 1px solid var(--el-border-color);
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}
}
</style>
